package com.apes.auto.car.service;

import com.apes.auto.car.model.CarMatchingRule;
import com.apes.auto.car.model.CarTypeConfiguration;
import com.apes.auto.car.repository.CarMatchingRuleRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

/**
 * @ClassName CarMatchingRuleService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/9/11 17:17
 * @Version 1.0
 **/
@Service("carMatchingRuleService")
public class CarMatchingRuleService extends DomainService {
    @Autowired
    private CarMatchingRuleRepository carMatchingRuleRepository;

    private static Map<String, String> SEAT_RULE = new HashMap<>();

    static {
        SEAT_RULE.put("5", "五座车");
        SEAT_RULE.put("7", "七座车");
        SEAT_RULE.put("5/6/7", "五座车;七座车");
        SEAT_RULE.put("5/6-7(选配)", "五座车;七座车");
        SEAT_RULE.put("5/7", "五座车;七座车");
        SEAT_RULE.put("5/7(选配)", "五座车;七座车");
        SEAT_RULE.put("5-7", "五座车;七座车");
        SEAT_RULE.put("5-7(选配)", "五座车;七座车");
        SEAT_RULE.put("7/5(选配)", "五座车;七座车");
        SEAT_RULE.put("4/5(选配)", "五座车;其它");
        SEAT_RULE.put("4-5", "五座车;其它");
        SEAT_RULE.put("5/4(选配)", "五座车;其它");
        SEAT_RULE.put("5/6", "五座车;其它");
        SEAT_RULE.put("5-6", "五座车;其它");
        SEAT_RULE.put("6/7", "七座车;其它");
        SEAT_RULE.put("6-7", "七座车;其它");
        SEAT_RULE.put("7(选配)/8", "七座车;其它");
        SEAT_RULE.put("7/6", "七座车;其它");
        SEAT_RULE.put("7/6(选配)", "七座车;其它");
        SEAT_RULE.put("7/8", "七座车;其它");
        SEAT_RULE.put("7-8", "七座车;其它");

    }

    public CarMatchingRule save(SimpleRequest request) {
        CarMatchingRule matchingRule = request.getO(CarMatchingRule.class);
        matchingRule.setTimestamp(DateUtil.format(new Date(), "yyyyMMdd.hhmmss.SSS"));
        return carMatchingRuleRepository.save(matchingRule);
    }

    public String delete(SimpleRequest request) {
        checkData(request);
        CarMatchingRule rule = request.getO(CarMatchingRule.class);
        carMatchingRuleRepository.delete(rule);
        return "删除成功";
    }

    public void checkData(SimpleRequest request) {
        CarMatchingRule rule = carMatchingRuleRepository.findOne((String) request.get("id"));
        long version = (int) request.get("version"); // 获取当前请求的版本信息
        if (null == rule) throw new RuntimeException("车型配件匹配规则【" + request.get("id") + "】:在系统中不存在");
        if (rule.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");

    }


    /**
     * 配件匹配
     *
     * @param car
     */
    public List<CarMatchingRule> syncMatchingRule(CarTypeConfiguration car) {
        List<CarMatchingRule> carMatchingRules = new ArrayList<>();
        IntStream.range(0, 9).forEach((i) -> {
            String produtCode = null;
            String productName = null;
            String fieldName = null;
            String chineseName = null;
            int matchingRule = 0;
            String matchingValue = "";
            switch (i) {
                case 0:
                    produtCode = "000609";
                    productName = "轮胎";
                    fieldName = "K_M01010102001LTGG";
                    chineseName = "轮胎规格";
                    matchingRule = 6;
                    if (null != car.getQLTGG()) {
                        matchingValue = car.getQLTGG().substring(0, car.getQLTGG().indexOf("R") + 3);
                    }
                    if (null != car.getHLGGG()) {
                        matchingValue = (matchingValue.equals("") ? "" : matchingValue + ";") + car.getHLTGG().substring(0, car.getHLTGG().indexOf("R") + 3);
                    }
                    break;
                case 1:
                    produtCode = "004918";
                    productName = "天窗防爆膜";
                    fieldName = "K_M01020204004TCLX";
                    chineseName = "天窗类型";
                    matchingRule = 0;
                    if ("有".equals(car.getQJTC()) || "选配".equals(car.getQJTC())) {
                        matchingValue = "全景天窗";
                    } else if ("有".equals(car.getDDTC()) || "选配".equals(car.getDDTC())) {
                        matchingValue = "单天窗";
                    }
                    break;
                case 2:
                    produtCode = "000849";
                    productName = "改装尾翼";
                    fieldName = "K_M01020203018CSXS";
                    chineseName = "车身型式";
                    matchingRule = 0;
                    if (null != car.getCSXS()) {
                        matchingValue = car.getCSXS() + "车";
                    }
                    break;
                case 3:
                    produtCode = "005298";
                    productName = "爆胎安全锁";
                    fieldName = "K_M01020205010LTCCC";
                    chineseName = "轮胎尺寸";
                    matchingRule = 5;
                    if (null != car.getQLGGG() && null != car.getHLGGG()) {
                        String QLGGG = car.getQLGGG().substring((car.getQLGGG().indexOf("×") == -1 ? car.getQLGGG().indexOf("x") : car.getQLGGG().indexOf("×")) + 1);
                        String HLGGG = car.getHLGGG().substring((car.getHLGGG().indexOf("×") == -1 ? car.getHLGGG().indexOf("x") : car.getHLGGG().indexOf("×")) + 1);

                        if (Integer.parseInt(QLGGG) < 12 || Integer.parseInt(QLGGG) > 22) {
                            QLGGG = "";
                        }
                        if (Integer.parseInt(HLGGG) < 12 || Integer.parseInt(HLGGG) > 22) {
                            HLGGG = "";
                        }
                        if (QLGGG.equals(HLGGG)) {
                            matchingValue = QLGGG;
                        } else {
                            matchingValue = QLGGG + ";" + HLGGG;
                        }
                    }
                    break;

                case 4:
                    produtCode = "000981";
                    productName = "坐垫";
                    fieldName = "K_M01020501015ZWS";
                    chineseName = "座位数";
                    matchingRule = 5;
                    matchingValue = "其他";
                    if (SEAT_RULE.containsKey(car.getZWSL())) {
                        matchingValue = SEAT_RULE.get(car.getZWSL());
                    }
                    break;
                case 5:
                    produtCode = "000820";
                    productName = "改装真皮座椅";
                    fieldName = "K_M01020202012ZWS";
                    chineseName = "座位数";
                    matchingRule = 0;
                    matchingValue = "其他";
                    if (SEAT_RULE.containsKey(car.getZWSL())) {
                        matchingValue = SEAT_RULE.get(car.getZWSL());
                    }
                    break;
                case 6:
                    produtCode = "000855";
                    productName = "防爆膜";
                    fieldName = "K_M01020204004ZWS";
                    chineseName = "座位数";
                    matchingRule = 5;
                    String carName = car.getCarType().getName();
                    if (!"房车".equals(carName) && !"客车".equals(carName) && !"货车".equals(carName)) {
                        if ("SUV".equals(carName)) {
                            matchingValue = "SUV";
                        } else if ("MPV".equals(carName)) {
                            matchingValue = "MPV";
                        } else if ("6".equals(car.getZWSL()) || "7".equals(car.getZWSL()) || "5/6".equals(car.getZWSL()) || "5-6".equals(car.getZWSL())) {
                            matchingValue = "七座车";
                        } else if ("3".equals(car.getZWSL()) || "2".equals(car.getZWSL()) || "4".equals(car.getZWSL()) || "5".equals(car.getZWSL()) || "4-5".equals(car.getZWSL()) || "5/4(选配)".equals(car.getZWSL())) {
                            matchingValue = "五座车";
                        } else if ("8".equals(car.getZWSL()) || "9".equals(car.getZWSL())) {
                            matchingValue = "其他";
                        }
                    }
                    break;
                case 7:
                    produtCode = "005579";
                    productName = "隐形车衣";
                    fieldName = "K_M01020203003ZWS";
                    chineseName = "座位数";
                    matchingRule = 5;
                    if (car.getZWSL() != null) {
                        //取座位数中的数字
                        String zws = Pattern.compile("[^0-9]").matcher(car.getZWSL()).replaceAll("");
                        if (Integer.parseInt(zws.substring(zws.length() - 1)) >= 5) {
                            matchingValue = "七座车";
                        } else {
                            matchingValue = "五座车";
                        }
                    }
                    if (car.getCSCD() != null) {
                        if (Integer.parseInt(car.getCSCD()) >= 5200) {
                            matchingValue = "大型车";
                        }
                    }
                    break;

                case 8:
                    produtCode = "000971";
                    productName = "脚垫";
                    fieldName = "K_M01020501007ZWS";
                    chineseName = "座位数";
                    matchingRule = 0;
                    if ("5".equals(car.getZWSL())) {
                        matchingValue = "五座车";
                    } else if ("7".equals(car.getZWSL())) {
                        matchingValue = "七座车";
                    } else {
                        matchingValue = "其他";
                    }

            }

            CarMatchingRule carMatchingRule = new CarMatchingRule();
            carMatchingRule.setCarId(car.getPZCXID());
            carMatchingRule.setProdutCode(produtCode);
            carMatchingRule.setProdutName(productName);
            carMatchingRule.setFieldName(fieldName);
            carMatchingRule.setChineseName(chineseName);
            carMatchingRule.setDataTypeCode("04");
            carMatchingRule.setDataTypeName("文本");
            carMatchingRule.setMatchingRule(matchingRule);
            carMatchingRule.setMatchingValue(matchingValue.replace(" ", ""));
            carMatchingRule.setTimestamp(DateUtil.format(new Date(), "yyyyMMdd.hhmmss.SSS"));
            carMatchingRules.add(carMatchingRule);
        });
        return carMatchingRules;

    }
}
